package org.example.controller;

import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.example.domain.ActivityRecord;
import org.example.domain.Admin;
import org.example.domain.User;
import org.example.service.impl.AdminServiceImpl;
import org.example.service.impl.UserServiceImpl;
import org.example.untils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminServiceImpl adminServiceImpl;

    @Autowired
    private UserServiceImpl userServiceImpl;

    @PostMapping("/login")
    public Result login(@RequestBody Admin admin){
        Admin admin1=adminServiceImpl.login(admin);
        if(admin1==null){
            return new Result(201,"账号或密码错误",null);
        } else {
            return new Result(200,"登入成功",admin1);
        }

    }

    @GetMapping("/getUserApplies")
    public Result getUserApplies(Integer start){//获取用户们的注册申请
        User[] users= userServiceImpl.getUserApplies(start);
        return new Result(200,"获取成功",users);
    }

    @GetMapping("/agreeRegister")
    public Result agreeRegister(Integer id){//通过用户们的注册申请
        userServiceImpl.agreeRegister(id);
        return new Result(200,"已通过",null);
    }

    @GetMapping("/disagreeRegister")
    public Result disagreeRegister(Integer id){//拒绝用户们的注册申请
        userServiceImpl.disagreeRegister(id);
        return new Result(200,"已拒绝",null);
    }

    @PostMapping("/registerByExcel")//管理员通过excel表格注册用户
    public Result registerByExcel(MultipartFile excel) throws IOException {
        int[] result=new int[3];
        Workbook workbook = new XSSFWorkbook(excel.getInputStream());
        Sheet sheet=workbook.getSheetAt(0);//获取表
        Row row;
        int i=1;
        row=sheet.getRow(i);//从第二行开始读取学号
        while (row!=null){
            String name= row.getCell(0).toString();//读取姓名
            StringBuilder account= new StringBuilder(row.getCell(1).toString().replaceAll("\\.", "").
                    replaceAll("E9", ""));//读取学号
            if(account.length()!=10){//尾号出现了0，对其循环补0
                for (int j=account.length();j<10;j++){
                    account.append("0");
                }
            }
            String className=row.getCell(2).toString();//读取班级
            boolean isOk=adminServiceImpl.registerByAdmin(account.toString(),account.substring(4,10),name,className);//是否注册成功
            if (isOk){
                result[1]++;//注册成功的数量
            } else {
                result[2]++;//注册失败的数量
            }
            i++;
            row=sheet.getRow(i);
        }
        result[0]=i-1;//共读取i-1条数据
        return new Result(200,"注册成功",result);
    }

    @PostMapping("/forceJoin")
    public Result forceJoin(@RequestBody ActivityRecord activityRecord){
        Integer result=adminServiceImpl.forceJoin(activityRecord.getUserId(),activityRecord.getActivityId());
        if(result==0){
            return new Result(201,"请勿重复报名",null);
        } else if(result==1){
            return new Result(200,"强制报名成功",null);
        } else {
            return new Result(201,"活动已签到",null);
        }
    }

    @PostMapping("/changeAdminInfo")//管理员修改个人信息
    public Result changeAdminInfo(@RequestBody Admin admin){
        adminServiceImpl.changeAdminInfo(admin);
        return new Result(200,"修改成功",null);
    }

    @GetMapping("/checkAdminOnePassword")
    public Result checkAdminOnePassword(@Param("password") String password){
        boolean result=adminServiceImpl.checkAdminOnePassword(password);
        if(result){
            return new Result(200,"密码校验通过",null);
        } else {
            return new Result(201,"密码校验失败",null);
        }
    }

    @GetMapping("/getNumberByUserWhereRegisterStatusEqualZero")
    public Result getNumberByUserWhereRegisterStatusEqualZero(){
        return new Result(200,"获取成功",adminServiceImpl.getNumberByUserWhereRegisterStatusEqualZero());
    }

    @GetMapping("/getNumberByEmailRecordWhereAdminIdEqualZero")
    public Result getNumberByEmailRecordWhereAdminIdEqualZero(){
        return new Result(200,"获取成功",adminServiceImpl.getNumberByEmailRecordWhereAdminIdEqualZero());
    }
}
